var custom = require("../../utils/custom.js");
var app = getApp();
Page({
  data: {
    carts: [
    ],
  
    flag: true,
    // input默认是1 
    count: 0,
    number: 1,
    minusStatuses: ['disabled', 'disabled', 'normal', 'normal', 'disabled'],
    type: '编辑',
    type1:[],
    apiurl: app.data.apiurl,
    openid: app.globalData.openid,
    typeindex:0,allprice:0,shopids:','
    
  },
  onShow: function () {
    var that = this
    wx.setNavigationBarTitle({
      title: '购物车'
    })
    var type = []
    for (let i = 0; i < that.data.carts.length; i++) {
      type[i] = '编辑'
    }
    that.setData({
      type1: type
    })
    //查询未支付的订单
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/shopping',
      data: { openid: app.globalData.openid, },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        that.setData({
          carts: res.data.data
        })
        // console.log(that.data.carts)
        // console.log(that.data.type1)
      }
    });
  },
  onLoad: function (options) {
    var that=this
    wx.setNavigationBarTitle({
      title: '购物车'
    })
    var type=[]
    for (let i = 0; i < that.data.carts.length;i++){
      type[i] = '编辑'
    }
    that.setData({
      type1:type
    })
    //查询未支付的订单
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/shopping',
      data: {openid: app.globalData.openid,},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
          that.setData({
            carts: res.data.data  
          })
          // console.log(that.data.carts)
          // console.log(that.data.type1)
      }  
    });
   
  },

  pay:function (){
    console.log(this.data.shopids);
    if(this.data.shopids==','){
      wx.showToast({
        title: '请选择商品',
        icon: 'clear',
        duration: 2000,
        image: '../../images/tanhao.png'
      });
    }else{
      
      wx.redirectTo({
        url: '../sorder/sorder?id=' + this.data.shopids + '&allprice=' + this.data.allprice,
        
      })
     
    }
  },
//删除

  b: function (b) {
   
    var that = this
    console.log(b);
    that.setData({
      oid: b.target.dataset.oid
    })
    wx.showModal({
      title: '提示',
      content: '是否删除',
      success: function (res) {
        if (res.confirm) {

          wx.request({
            url: getApp().data.apiurl + '/index.php/index/index/delShopOrder',
            data: { oid: b.target.dataset.oid, openid: app.globalData.openid },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              // console.log(res);
              wx.request({
                url: getApp().data.apiurl + '/index.php/index/index/shopping',
                data: { openid: app.globalData.openid, },
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  console.log(res)
                  that.setData({
                    carts: res.data.data
                  })
                  // console.log(that.data.carts)
                  // console.log(that.data.type1)
                }
              });
            }
          });
         }//end if
      }
    });
   
  },
  bindCheckbox: function (e) {
    /*绑定点击事件，将checkbox样式改变为选中与非选中*/
    //拿到下标值，以在carts作遍历指示用
    var index = parseInt(e.currentTarget.dataset.index);
    //原始的icon状态
    var selected = this.data.carts[index].selected;
    var carts = this.data.carts;
    // 对勾选状态取反
    carts[index].selected = !selected;
    if (selected){
      this.data.allprice -= this.formatFloat(e.currentTarget.dataset.num * e.currentTarget.dataset.oprice);
      this.data.shopids = this.data.shopids.replace(',' + e.currentTarget.dataset.id + ',', ',');
    } else {
      this.data.allprice += this.formatFloat(e.currentTarget.dataset.num * e.currentTarget.dataset.oprice);
      this.data.shopids += e.currentTarget.dataset.id + ',';
    }

    console.log(e.currentTarget);    // 写回经点击修改后的数组
    this.setData({
      carts: carts, 
      allprice: this.formatFloat(this.data.allprice), shopids: this.data.shopids
    });
  },

  bindSelectAll: function () {
    // 环境中目前已选状态
    var selectedAllStatus = this.data.selectedAllStatus;
    // 取反操作
    selectedAllStatus = !selectedAllStatus;
    // 购物车数据，关键是处理selected值
    var carts = this.data.carts;
    // 遍历
    var aprice=0;
    var shopids=',';
    for (var i = 0; i < carts.length; i++) {
      carts[i].selected = selectedAllStatus;
      aprice += this.formatFloat(carts[i].num * carts[i].price);
      shopids += carts[i].id + ',';
    }
    if (!selectedAllStatus){
      aprice=0;
      shopids=',';
    }
    this.setData({
      selectedAllStatus: selectedAllStatus,
      carts: carts, 
      allprice: this.formatFloat(aprice), 
      shopids: shopids
    });
  },

  /* 点击减号 */
  bindMinus: function (e) {
    var index = parseInt(e.currentTarget.dataset.index);
    var num = this.data.carts[index].num;
    if (num > 1) {
      num--;
    }
    var price=this.data.carts[index].price;
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    var carts = this.data.carts;
    carts[index].num = num;
    
    var minusStatuses = this.data.minusStatuses;
    minusStatuses[index] = minusStatus;
    this.setData({
      carts: carts,
      minusStatuses: minusStatuses,
      allprice: this.formatFloat(this.data.allprice-price),
    });
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/saveNum',
      data: { id:this.data.carts[index].id , num:this.data.carts[index].num },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        
      }
    });
   
  },
  bindPlus: function (e) {
    var index = parseInt(e.currentTarget.dataset.index);
    var num = this.data.carts[index].num;
    num++;
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    var carts = this.data.carts;
    carts[index].num = num;
    var price = this.formatFloat(this.data.carts[index].price);
    var minusStatuses = this.data.minusStatuses;
    minusStatuses[index] = minusStatus;

    this.setData({
      carts: carts,
      minusStatuses: minusStatuses,
      allprice: this.formatFloat(this.data.allprice+price),
      
    });
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/saveNum',
      data: { id: this.data.carts[index].id, num: this.data.carts[index].num },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
       
      }
    });
  }, 


  toggle: function (e) {
    var that = this;
   // console.log(e)
    var type1=that.data.carts
    that.data.typeindex = e.currentTarget.dataset.index
    console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index
    var type = type1[index].type === '编辑' ? '保存' : '编辑';
    type1[index].type=type
    //console.log(type1[index].type),
    that.setData({
      carts: type1
    });
   // console.log(that.data.carts)
  },
  formatFloat: function (f) {
    var m = Math.pow(10, 1);
    return parseInt(f * m, 10) / m;
  } 
  
})
